package com.zhihuichongcheng.application.ui.im.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.zhihuichongcheng.application.R;
import com.zhihuichongcheng.application.bean.mine.UserBean;
import com.zhihuichongcheng.application.glide.GlideCircleTransform;
import com.zhihuichongcheng.application.ui.im.activity.ChatActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 3water on 2016/10/27.
 */

public class ContactSearchAdapter extends RecyclerView.Adapter<ContactSearchAdapter.ViewHold> {
    private Context mContext;
    private List<UserBean> mListData = new ArrayList<>();
    public ContactSearchAdapter(Context context)
    {
        this.mContext = context;
    }

    @Override
    public ViewHold onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.listadapter_contact,null);
        return new ViewHold(view);
    }

    @Override
    public void onBindViewHolder(ViewHold holder, int position) {
        final UserBean easeUser = mListData.get(position);
        holder.mTvName.setText(easeUser.getUserName());
//        final DepInfo info = easeUser.getDept();
//        if(info!=null)
//        {
//            DepInfo preInfo = null;
//            if(position>0)
//            {
//             preInfo = mListData.get(position-1).getDept();
//            }
//            String preHead ="";
//            if(preInfo!=null)
//            {
//                preHead  = preInfo.getDeptName();
//
//            }
//
//            String header = info.getDeptName();
//            if (position == 0 ||TextUtils.isEmpty(preHead)||header != null && !header.equals(preHead)) {
//                if (TextUtils.isEmpty(header)) {
//                    holder.mTvHeader.setVisibility(View.GONE);
//                } else {
//                    holder.mTvHeader.setVisibility(View.VISIBLE);
//                    holder.mTvHeader.setText(header);
//                }
//            } else {
//                holder.mTvHeader.setVisibility(View.GONE);
//            }
//        }
//        else
//        {
//            holder.mTvHeader.setVisibility(View.GONE);
//        }
        Glide.with(mContext).load(easeUser.getLogoUrl()).placeholder(R.drawable.ic_default_avatar).transform(new GlideCircleTransform(mContext)).into(holder.mImAvatar);

        holder.view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ChatActivity.class);
                intent.putExtra("userId",easeUser.getEasemobNumber());
                intent.putExtra("username",easeUser.getUserName());
                mContext.startActivity(intent);
                ((Activity)mContext).overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
            }
        });
    }

    public void setData(List<UserBean> list)
    {
        this.mListData = list;
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    class ViewHold extends RecyclerView.ViewHolder{
        private TextView mTvHeader,mTvName;
        private ImageView mImAvatar;
        private View view;
        public ViewHold(View itemView) {
            super(itemView);
            view = itemView;
            mTvHeader = (TextView)itemView.findViewById(R.id.header);
            mImAvatar = (ImageView)itemView.findViewById(R.id.avatar);
            mTvName = (TextView)itemView.findViewById(R.id.name);
        }
    }
}
